package org.ddd.orderdemo.repository.adapter;

import org.ddd.orderdemo.domain.infra.repository.IOrderRepository;
import org.ddd.orderdemo.domain.model.order.Order;
import org.ddd.orderdemo.repository.convertor.OrderConvertor;
import org.ddd.orderdemo.repository.jpa.OrderJpaRepository;
import org.ddd.orderdemo.repository.po.OrderPo;
import org.springframework.data.domain.Example;

import java.util.List;
import java.util.Optional;

public class OrderJpaRepositoryAdapter implements IOrderRepository {

    private OrderJpaRepository orderJpaRepository;

    @Override
    public void createOrder(Order order) {

        OrderPo orderPo = OrderConvertor.convertToPo(order);

        OrderPo save = orderJpaRepository.save(orderPo);

    }

    @Override
    public Order loadOrder(String orderNo) {

        OrderPo orderPo = new OrderPo();
        orderPo.setOrderNo(orderNo);

        //将匹配对象封装成Example对象
        Example<OrderPo> example =Example.of(orderPo);

        Optional<OrderPo> optionalOrderPo = orderJpaRepository.findOne(example);
//        Optional<OrderPo> optionalOrderPo = orderJpaRepository.findById(orderNo);

        Order order = OrderConvertor.convertToEntity(optionalOrderPo.get());

        return order;
    }

    @Override
    public List<Order> getOrders(List<String> orderNos) {
        return List.of();
    }
}
